home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap04 / SysMets3 / SysMets3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  8.4 KB  |  270 lines

  1. /*----------------------------------------------------
  2.    SYSMETS3.C -- System Metrics Display Program No. 3
  3.                  (c) Charles Petzold, 1998
  4.   ----------------------------------------------------*/
  5.  
  6. #define WINVER 0x0500
  7. #include <windows.h>
  8. #include "sysmets.h"
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR szCmdLine, int iCmdShow)
  14. {
  15.      static TCHAR szAppName[] = TEXT ("SysMets3") ;
  16.      HWND         hwnd ;
  17.      MSG          msg ;
  18.      WNDCLASS     wndclass ;
  19.      
  20.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  21.      wndclass.lpfnWndProc   = WndProc ;
  22.      wndclass.cbClsExtra    = 0 ;
  23.      wndclass.cbWndExtra    = 0 ;
  24.      wndclass.hInstance     = hInstance ;
  25.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  26.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  27.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  28.      wndclass.lpszMenuName  = NULL ;
  29.      wndclass.lpszClassName = szAppName ;
  30.      
  31.      if (!RegisterClass (&wndclass))
  32.      {
  33.           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
  34.                       szAppName, MB_ICONERROR) ;
  35.           return 0 ;
  36.      }
  37.      
  38.      hwnd = CreateWindow (szAppName, TEXT ("Get System Metrics No. 3"),
  39.                           WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.      
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.      
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.      {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.      }
  52.      return msg.wParam ;
  53. }
  54.  
  55. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  56. {
  57.      static int  cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth ;
  58.      HDC         hdc ;
  59.      int         i, x, y, iVertPos, iHorzPos, iPaintBeg, iPaintEnd ;
  60.      PAINTSTRUCT ps ;
  61.      SCROLLINFO  si ;
  62.      TCHAR       szBuffer[10] ;
  63.      TEXTMETRIC  tm ;
  64.      
  65.      switch (message)
  66.      {
  67.      case WM_CREATE:
  68.           hdc = GetDC (hwnd) ;
  69.           
  70.           GetTextMetrics (hdc, &tm) ;
  71.           cxChar = tm.tmAveCharWidth ;
  72.           cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
  73.           cyChar = tm.tmHeight + tm.tmExternalLeading ;
  74.           
  75.           ReleaseDC (hwnd, hdc) ;
  76.  
  77.                // Save the width of the three columns
  78.           
  79.           iMaxWidth = 40 * cxChar + 22 * cxCaps ;
  80.           return 0 ;
  81.           
  82.      case WM_SIZE:
  83.           cxClient = LOWORD (lParam) ;
  84.           cyClient = HIWORD (lParam) ;
  85.  
  86.                // Set vertical scroll bar range and page size
  87.  
  88.           si.cbSize = sizeof (si) ;
  89.           si.fMask  = SIF_RANGE | SIF_PAGE ;
  90.           si.nMin   = 0 ;
  91.           si.nMax   = NUMLINES - 1 ;
  92.           si.nPage  = cyClient / cyChar ;
  93.           SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
  94.  
  95.                // Set horizontal scroll bar range and page size
  96.  
  97.           si.cbSize = sizeof (si) ;
  98.           si.fMask  = SIF_RANGE | SIF_PAGE ;
  99.           si.nMin   = 0 ;
  100.           si.nMax   = 2 + iMaxWidth / cxChar ;
  101.           si.nPage  = cxClient / cxChar ;
  102.           SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
  103.           return 0 ;
  104.           
  105.      case WM_VSCROLL:
  106.                // Get all the vertial scroll bar information
  107.  
  108.           si.cbSize = sizeof (si) ;
  109.           si.fMask  = SIF_ALL ;
  110.           GetScrollInfo (hwnd, SB_VERT, &si) ;
  111.  
  112.                // Save the position for comparison later on
  113.  
  114.           iVertPos = si.nPos ;
  115.  
  116.           switch (LOWORD (wParam))
  117.           {
  118.           case SB_TOP:
  119.                si.nPos = si.nMin ;
  120.                break ;
  121.                
  122.           case SB_BOTTOM:
  123.                si.nPos = si.nMax ;
  124.                break ;
  125.                
  126.           case SB_LINEUP:
  127.                si.nPos -= 1 ;
  128.                break ;
  129.                
  130.           case SB_LINEDOWN:
  131.                si.nPos += 1 ;
  132.                break ;
  133.                
  134.           case SB_PAGEUP:
  135.                si.nPos -= si.nPage ;
  136.                break ;
  137.                
  138.           case SB_PAGEDOWN:
  139.                si.nPos += si.nPage ;
  140.                break ;
  141.                
  142.           case SB_THUMBTRACK:
  143.                si.nPos = si.nTrackPos ;
  144.                break ;
  145.                
  146.           default:
  147.                break ;         
  148.           }
  149.                // Set the position and then retrieve it.  Due to adjustments
  150.                //   by Windows it may not be the same as the value set.
  151.  
  152.           si.fMask = SIF_POS ;
  153.           SetScrollInfo (hwnd, SB_VERT, &si, TRUE) ;
  154.           GetScrollInfo (hwnd, SB_VERT, &si) ;
  155.  
  156.                // If the position has changed, scroll the window and update it
  157.  
  158.           if (si.nPos != iVertPos)
  159.           {                    
  160.                ScrollWindow (hwnd, 0, cyChar * (iVertPos - si.nPos), 
  161.                                    NULL, NULL) ;
  162.                UpdateWindow (hwnd) ;
  163.           }
  164.           return 0 ;
  165.           
  166.      case WM_HSCROLL:
  167.                // Get all the vertial scroll bar information
  168.  
  169.           si.cbSize = sizeof (si) ;
  170.           si.fMask  = SIF_ALL ;
  171.  
  172.                // Save the position for comparison later on
  173.  
  174.           GetScrollInfo (hwnd, SB_HORZ, &si) ;
  175.           iHorzPos = si.nPos ;
  176.  
  177.           switch (LOWORD (wParam))
  178.           {
  179.           case SB_LINELEFT:
  180.                si.nPos -= 1 ;
  181.                break ;
  182.                
  183.           case SB_LINERIGHT:
  184.                si.nPos += 1 ;
  185.                break ;
  186.                
  187.           case SB_PAGELEFT:
  188.                si.nPos -= si.nPage ;
  189.                break ;
  190.                
  191.           case SB_PAGERIGHT:
  192.                si.nPos += si.nPage ;
  193.                break ;
  194.                
  195.           case SB_THUMBPOSITION:
  196.                si.nPos = si.nTrackPos ;
  197.                break ;
  198.                
  199.           default :
  200.                break ;
  201.           }
  202.                // Set the position and then retrieve it.  Due to adjustments
  203.                //   by Windows it may not be the same as the value set.
  204.  
  205.           si.fMask = SIF_POS ;
  206.           SetScrollInfo (hwnd, SB_HORZ, &si, TRUE) ;
  207.           GetScrollInfo (hwnd, SB_HORZ, &si) ;
  208.           
  209.                // If the position has changed, scroll the window 
  210.  
  211.           if (si.nPos != iHorzPos)
  212.           {
  213.                ScrollWindow (hwnd, cxChar * (iHorzPos - si.nPos), 0, 
  214.                              NULL, NULL) ;
  215.           }
  216.           return 0 ;
  217.  
  218.      case WM_PAINT :
  219.           hdc = BeginPaint (hwnd, &ps) ;
  220.  
  221.                // Get vertical scroll bar position
  222.  
  223.           si.cbSize = sizeof (si) ;
  224.           si.fMask  = SIF_POS ;
  225.           GetScrollInfo (hwnd, SB_VERT, &si) ;
  226.           iVertPos = si.nPos ;
  227.  
  228.                // Get horizontal scroll bar position
  229.  
  230.           GetScrollInfo (hwnd, SB_HORZ, &si) ;
  231.           iHorzPos = si.nPos ;
  232.  
  233.                // Find painting limits
  234.  
  235.           iPaintBeg = max (0, iVertPos + ps.rcPaint.top / cyChar) ;
  236.           iPaintEnd = min (NUMLINES - 1,
  237.                            iVertPos + ps.rcPaint.bottom / cyChar) ;
  238.           
  239.           for (i = iPaintBeg ; i <= iPaintEnd ; i++)
  240.           {
  241.                x = cxChar * (1 - iHorzPos) ;
  242.                y = cyChar * (i - iVertPos) ;
  243.                
  244.                TextOut (hdc, x, y,
  245.                         sysmetrics[i].szLabel,
  246.                         lstrlen (sysmetrics[i].szLabel)) ;
  247.                
  248.                TextOut (hdc, x + 22 * cxCaps, y,
  249.                         sysmetrics[i].szDesc,
  250.                         lstrlen (sysmetrics[i].szDesc)) ;
  251.                
  252.                SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
  253.                
  254.                TextOut (hdc, x + 22 * cxCaps + 40 * cxChar, y, szBuffer,
  255.                         wsprintf (szBuffer, TEXT ("%5d"),
  256.                              GetSystemMetrics (sysmetrics[i].iIndex))) ;
  257.                
  258.                SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
  259.           }
  260.  
  261.           EndPaint (hwnd, &ps) ;
  262.           return 0 ;
  263.           
  264.      case WM_DESTROY :
  265.           PostQuitMessage (0) ;
  266.           return 0 ;
  267.      }
  268.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  269. }
  270.